package controller.book;

import com.alibaba.fastjson.JSONObject;
import dao.DaoException;
import dao.book.BookDaoImp;
import model.Book;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/ShowBooksServlet")
public class ShowBooksServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        BookDaoImp dao = new BookDaoImp();
        String state = request.getParameter("state");
        if ("showAll".equals(state)) {

            String pattern = request.getParameter("key[id]");
            int pageNum = Integer.parseInt(request.getParameter("page"));
            int pageSize = Integer.parseInt(request.getParameter("limit"));
            ArrayList<Book> books = null;
            int count = 0;
            if (pattern != null) {
                try {
                    books = dao.searchBooksByPageAll(pattern, pageNum, pageSize);
                    count = dao.searchBookNumAll(pattern);
                } catch (DaoException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    count = dao.findBookNumAll();
                    books = dao.findBooksByPageAll(pageNum, pageSize);

                } catch (DaoException e) {
                    e.printStackTrace();
                }
            }
            Map<String, Object> map = new HashMap<>();
            map.put("code", 0);
            map.put("msg", "");
            map.put("count", count);
            map.put("data", books);

            JSONObject json = new JSONObject(map);
            response.setContentType("application/json;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.println(json.toJSONString());
        } else if ("showBorrow".equals(state)) {
            String pattern = request.getParameter("key[id]");
            int pageNum = Integer.parseInt(request.getParameter("page"));
            int pageSize = Integer.parseInt(request.getParameter("limit"));
            ArrayList<Book> books = null;
            int count = 0;
            if (pattern != null) {
                try {
                    books = dao.searchBooksByPageBorrow(pattern, pageNum, pageSize);
                    count = dao.searchBookNumBorrow(pattern);
                } catch (DaoException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    count = dao.findBookNumBorrow();
                    books = dao.findBooksByPageBorrow(pageNum, pageSize);

                } catch (DaoException e) {
                    e.printStackTrace();
                }
            }
            Map<String, Object> map = new HashMap<>();
            map.put("code", 0);
            map.put("msg", "");
            map.put("count", count);
            map.put("data", books);

            JSONObject json = new JSONObject(map);
            response.setContentType("application/json;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.println(json.toJSONString());

        }
    }
}
